import java.util.Deque;
import java.util.LinkedList;

/**
 * 513. 找树左下角的值
 */
public class No513 {
    public int findBottomLeftValue(TreeNode root) {
        int answer = 0;
        Deque<TreeNode> deque = new LinkedList<>();
        if (root != null) {
            deque.add(root);
            answer = root.val;
        }
        while (!deque.isEmpty()) {
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.poll();
                if (node.left != null) deque.add(node.left);
                if (node.right != null) deque.add(node.right);
            }
            if (!deque.isEmpty()) answer = deque.peek().val;
        }
        return answer;
    }
}
